<?php

	function attach($attach, $type) {
		if (false === strpos($attach, 'http://')) {
			//本地附件
			return __ROOT__ . '/' . C('pin_attach_path') . $type . '/' . $attach;
			//远程附件
			//todo...
		} else {
			//URL链接
			return $attach;
		}
	}
    
	function update_config($new_config, $config_file = '') {
        !is_file($config_file) && $config_file = CONF_PATH . 'Home/config.php';
        if (is_writable($config_file)) {
            $config = require $config_file;
            $config = array_merge($config, $new_config);
            file_put_contents($config_file, "<?php \nreturn " . stripslashes(var_export($config, true)) . ";", LOCK_EX);
            @unlink(RUNTIME_FILE);
            return true;
        } else {
            return false;
        }
    }
	
	function Y($val,$condition){
		if($_GET){
			$val=$_GET["$val"];
		}
		if($_POST){
			$val=$_POST["$val"];
		}
		$data=$condition($val);
		return $data;
	}

	//递归重组节点多维数组
	function nodeMerge($node,$pid=0){
		$arr=array();
		foreach($node as $v){
			if($v['pid']==$pid){
				$v['child']=nodeMerge($node,$v['id']);
				$arr[]=$v;
			}
		}
		return $arr;
	}

	//递归权限中的多维数组
	function accessMerge($node,$access=null,$pid=0){
		$arr=array();
		foreach($node as $v){
			if(is_array($access)){
				$v['access']=in_array($v['id'],$access)? 1 : 0;
			}
		
			if($v['pid']==$pid){
				$v['child']=accessMerge($node,$access,$v['id']);
				$arr[]=$v;
			}
		}
		return $arr;
	}


function getArticleName($id=0,$all=false,$field='title,site_id'){
    if($id){
        $info = M('article')->where(array('id'=>$id))->field($field)->find();
        return $all ? $info : $info['title'];
    }else{
        return '';
    }

}
function getModel($key=''){
    $arr = array(
        'house' => '新房',
        'used' => '二手房',
        'rental' => '出租房',
        'estate' => '小区',
    );
    if($key){
        return array_key_exists($key,$arr) ? $arr[$key] : '';
    }else{
        return $arr;
    }
}

function setBusinessType($key = ''){
    $arr = array(
      '1' => '商铺',
        '2' => '写字楼'
    );
    if($key){
        return array_key_exists($key,$arr) ? $arr[$key] : '';
    }else{
        return $arr;
    }
}

function getCateName($cateid){
    $obj = D("ArticleCate");
    $info = $obj->where(array('id'=>$cateid))->getField('name');
    return $info;
}

function string2array($data) {
    if($data == '') return array();
    $data = stripslashes($data);
    @eval("\$array = $data;");
    return $array;
}
/**
 * 将数组转换为字符串
 *
 * @param	array	$data		数组
 * @param	bool	$isformdata	如果为0，则不使用new_stripslashes处理，可选参数，默认为1
 * @return	string	返回字符串，如果，data为空，则返回空
 */
function array2string($data, $isformdata = 1) {
    if($data == '') return '';
    if($isformdata) $data = new_stripslashes($data);
    return addslashes(var_export($data, TRUE));
}

function new_addslashes($string){
    if(!is_array($string)) return addslashes($string);
    foreach($string as $key => $val) $string[$key] = new_addslashes($val);
    return $string;
}

/**
 * 返回经stripslashes处理过的字符串或数组
 * @param $string 需要处理的字符串或数组
 * @return mixed
 */
function new_stripslashes($string) {
    if(!is_array($string)) return stripslashes($string);
    foreach($string as $key => $val) $string[$key] = new_stripslashes($val);
    return $string;
}
/**
 * 创建目录
 *
 * @param	string	$path	路径
 * @param	string	$mode	属性
 * @return	string	如果已经存在则返回true，否则为flase
 */
function dir_create($path, $mode = 0777) {
    if(is_dir($path)) return TRUE;
    $ftp_enable = 0;
    $path = dir_path($path);
    $temp = explode('/', $path);
    $cur_dir = '';
    $max = count($temp) - 1;
    for($i=0; $i<$max; $i++) {
        $cur_dir .= $temp[$i].'/';
        if (@is_dir($cur_dir)) continue;
        @mkdir($cur_dir, 0777,true);
        @chmod($cur_dir, 0777);
    }
    return is_dir($path);
}
function dir_path($path) {
    $path = str_replace('\\', '/', $path);
    if(substr($path, -1) != '/') $path = $path.'/';
    return $path;
}
/**
 * 取得文件扩展
 *
 * @param $filename 文件名
 * @return 扩展名
 */
function fileext($filename) {
    return strtolower(trim(substr(strrchr($filename, '.'), 1, 10)));
}
/**
 * 返回经htmlspecialchars处理过的字符串或数组
 * @param $obj 需要处理的字符串或数组
 * @return mixed
 */
function new_html_special_chars($string) {
    $encoding = 'utf-8';
    if(strtolower(CHARSET)=='gbk') $encoding = 'ISO-8859-15';
    if(!is_array($string)) return htmlspecialchars($string,ENT_QUOTES,$encoding);
    foreach($string as $key => $val) $string[$key] = new_html_special_chars($val);
    return $string;
}

//获取分类下的人员
function classify ($level, $scope){
    $users = '';
    switch ($level){
        case 0:
            $users = D ('Division')->get_users_group ($scope);
            break;
        case 1:
            $users = D ('Division')->get_users_shop ($scope);
            break;
        case 2:
            $users = get_users_area ($scope, 2);
            break;
        case 3:
            $users = get_users_area ($scope, 3);
            break;
        case 4:
            $users = get_users_area ($scope, 4);
            break;
        case 5:
            $users = get_users_area ($scope, 5);
            break;
        default:
            break;
    }
    return trim($users, ',');
}
//获取区域下的经纪人
function get_users_area ($area_id, $level){
    $area_ids = '';
    if ($level == 2){
        $area_ids .= $area_id;
    }
    if ($level == 3){
        $area_ids = ''.$area_id.',';
        $ids = M ('area_cate')->where ('pid='.$area_id)->getField('id', true);
        $area_ids .= implode(',', $ids);
    }
    if ($level == 4){
        $p_area_ids = '';
        $sp_area_ids = '';
        $ids = M ('area_cate')->where ('pid='.$area_id)->getField('id', true);
        $p_area_ids .= implode(',', $ids);
        $p_ids = M ('area_cate')->where (array ('pid'=>array ('in', trim($area_ids, ','))))->getField('id', true);
        $sp_area_ids .= implode(',', $p_ids);
        $area_ids .= $p_area_ids.$sp_area_ids.$area_id;
    }
    if ($level == 5){
        $ids = M ('area_cate')->getField('id', true);
        $area_ids .= implode(',', $ids);
    }
    $area_ids = trim ($area_ids, ',');
    //查找店铺
    $shops = D ('shop')->get_shops ($area_ids);

    //查找经纪人
    $usesr = D ('division')->get_users_shop ($shops);
    return $usesr;


}
//获取分类下子分类
function subclassification ($level){
    $users = '';
    switch ($level){
        case 0:
            $users = D ('Division')->get_users_group ($scope);
            break;
        case 1:
            $users = D ('Division')->get_users_shop ($scope);
            break;
        case 2:
            $users = get_users_area ($scope, 2);
            break;
        case 3:
            $users = get_users_area ($scope, 3);
            break;
        case 4:
            $users = get_users_area ($scope, 4);
            break;
        case 5:
            $users = get_users_area ($scope, 5);
            break;
        default:
            break;
    }
    return trim($users, ',');
}